package com.tanger.auth.authorization.service;

import com.tanger.auth.mapper.AuthMapper;
import com.tanger.common.vo.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Description TODO
 * @Author Hchenbin
 * @Date 2022/6/14 10:32
 * @Version 1.0
 */
@Service
public class UserDetailsServiceHandle implements UserDetailsService {

    @Autowired
    private AuthMapper authMapper;

    @Override
    public UserDetails loadUserByUsername(String username) {
        //TODO 用户角色
        List<String> roles = Arrays.asList(new String[]{"ADMIN","USER"});
        UserDetail userDetail = authMapper.queryUserInfoByPhone(username);
        if (userDetail == null) {
            throw new UsernameNotFoundException(String.format("找不到此用户名%s。", username));
        } else {
            return new User(username, userDetail.getPassword(), AuthorityUtils.createAuthorityList((String[])roles.toArray()));
        }
    }
}
